<?php

namespace App\Models\Api;

use Site;

class CompanyRefreshLog extends ApiModel
{
    protected $table = 'company_refresh_log';

    public $timestamps = false;

    public static $remainingTimes = 0;

    /** 判断是否可以刷新职位
     * @param $user
     * @return bool
     */
    public static function isRefresh($user)
    {
        $getMax = self::getMax($user);
        if ($getMax < 0) {
            return false;
        } elseif ($getMax == 0) {
            return true;
        } else {
            $count = self::where('refresh_time', '>', date('Y-m-d 00:00:00'))
                ->where('uid', $user->uid)
                ->count();

            $bl = $count >= $getMax;

            self::$remainingTimes = $bl ? 0 : $getMax - $count - 1;

            return !$bl;
        }
    }

    /** 返回当前会员今天最大刷新数 -1不能刷新  0不限  正数代表最大次数
     * @param $user
     * @return mixed
     */
    public static function getMax($user)
    {
        if ($user->viptime >= date('Y-m-d H:i:s')) {//vip
            return Site::get('vip_refresh_number');
        }
        return Site::get('member_refresh_number');
    }
}
